package com.terra;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import com.lamerman.FileDialog;

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnPreferenceClickListener, OnClickListener {

private ListPreference mListPreference;
private CheckBoxPreference mAutoloadlastFilePreference;
private EditTextPreference mLocalHTMLPreference;
private EditTextPreference mTCUsernamePreference;
//private EditTextPreference mTCPasswordPreference;

public static final String START_PATH = "START_PATH";
public static final String RESULT_PATH = "RESULT_PATH";
public static final String PATH = "PATH";
public static final String PATHONLY = "true";
public static final String LOADSAVE = "LOADSAVE";
public static final int REQUEST_SAVE = 1;
public static final int REQUEST_LOAD = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        addPreferencesFromResource(R.layout.preferences);
        
        //hide reset preference

        CheckBoxPreference mReset = (CheckBoxPreference)getPreferenceScreen().findPreference("Reset");
        PreferenceCategory mResetCat = (PreferenceCategory) findPreference("category_hidden");

        EditTextPreference lastFile = (EditTextPreference)getPreferenceScreen().findPreference("LastFile");
        mResetCat.removePreference(mReset);
        mResetCat.removePreference(lastFile);
        
        mListPreference = (ListPreference)getPreferenceScreen().findPreference("unitPref");
        mAutoloadlastFilePreference = (CheckBoxPreference)getPreferenceScreen().findPreference("LoadLastFile");
        mLocalHTMLPreference = (EditTextPreference)getPreferenceScreen().findPreference("LocalHTMLPath");
        mTCUsernamePreference = (EditTextPreference)getPreferenceScreen().findPreference("Username");
       // mTCPasswordPreference = (EditTextPreference)getPreferenceScreen().findPreference("Password");
        
        mLocalHTMLPreference.setOnPreferenceClickListener(this);
}

	@Override
	protected void onResume() {
	    super.onResume();

	    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	    
		SetSummaries();
	}

	@Override
	protected void onPause() {
	    super.onPause();
	
	    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
		//Globals.OnPause();
	}

	public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {

		Globals.OnResume();
	    SetSummaries();
	}

	void SetSummaries() {

		if( Globals.strUnits.equalsIgnoreCase("English") )
			mListPreference.setSummary("Units set to English");
		else
			mListPreference.setSummary("Units set to Metric");
	
		if( Globals.bLoadLastFile )
			mAutoloadlastFilePreference.setSummary("Autoloading last gpx file");
		else
			mAutoloadlastFilePreference.setSummary("Not autoloading last gpx file");

		mTCUsernamePreference.setSummary("TC.com username "+Globals.strUsername);
		mLocalHTMLPreference.setSummary("Local HTML at "+Globals.strLocalHTMLPath);
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {

        if (preference.getTitle().equals("Local HTML Path")) {
    		Intent fileIntent = new Intent(this, FileDialog.class);
			fileIntent.putExtra(FileDialog.START_PATH, Globals.strLocalHTMLPath);
			fileIntent.putExtra(FileDialog.LOADSAVE, "LOAD");
			fileIntent.putExtra("PATHONLY", "true");
    		startActivityForResult(fileIntent, REQUEST_SAVE);
        }

        return false;
	}
	
    public synchronized void onActivityResult(final int requestCode,
            int resultCode, final Intent data) {
    	
            if (resultCode == Activity.RESULT_OK) {

                        String filePath = data.getStringExtra(FileDialog.PATH);
                        Log.v("Debug", "File "+filePath);

                        mLocalHTMLPreference.getEditText().setText(filePath);
                        SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(this);
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.putString("localHTML", filePath);
                        editor.commit();
            } else if (resultCode == Activity.RESULT_CANCELED) {
            	Log.v("FD", "Request cancelled");
            }
    }
    
	@Override
	public void onClick(View v) {
		Intent fileIntent = new Intent(this, FileDialog.class);
//		fileIntent.putExtra(FileDialog.START_PATH, Globals.strUserPath);
		fileIntent.putExtra(FileDialog.START_PATH, "//sdcard//");
		
		fileIntent.putExtra(FileDialog.LOADSAVE, "LOAD");
		fileIntent.putExtra("PATHONLY", "false");
		startActivityForResult(fileIntent, FileDialog.REQUEST_LOAD);
		
	}

}
